//
//  JYPicPickerView.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/5.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit

private let edgeMargin : CGFloat = 15
private let JYPicPickerCell = "picPicker"

class JYPicPickerView: UICollectionView {
    // MARK:- 自定义属性
    var images : [UIImage] = [UIImage]() {
        didSet {
            reloadData()
        }
    }
    

    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置数据源
        dataSource = self
        
        //设置布局
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemWH = (UIScreen.mainScreen().bounds.width - 4 * edgeMargin) / 3
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = edgeMargin
        layout.minimumInteritemSpacing = edgeMargin
        
        //设置内边距
        contentInset = UIEdgeInsets(top: edgeMargin, left: edgeMargin, bottom: edgeMargin, right: edgeMargin)
        
        //注册cell
        registerNib(UINib(nibName: "JYPicPickerViewCell", bundle: nil), forCellWithReuseIdentifier: JYPicPickerCell)
        

    }

}

// MARK:- 数据源
extension JYPicPickerView : UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(JYPicPickerCell, forIndexPath: indexPath) as! JYPicPickerViewCell
        
        cell.image = indexPath.row < images.count ? images[indexPath.row] : nil
        
        return cell
        
    }
}